<template>
	<view class="container">
		<view class="projects">
			<block v-for="(item,index) in project">
				<view class="item" :class="index == projectIndex ? 'active' : ''" @click="changeProject(index)">{{item.name}}</view>
			</block>
		</view>
		<view v-if="current == 2" class="last" @click="last">
			<view>本周<uni-icons type="right"></uni-icons></view>
		</view>
		<view class="next" @click="next" v-if="current == 1">
			<view >下一周<uni-icons type="right"></uni-icons></view>
		</view>
		<view class="list-box">
			<block v-if="week.length > 0" v-for="(item,index) in week">
				<view class="col">
					<view class="head">
						<view class="row date">{{item.date}}</view>
						<view class="row">{{item.weekName}}</view>
					</view>
					<block v-if="projectIndex == 0">
						<view class="time-item" @click="setContent" :data-index="index" :data-item="item" data-type="0" :class="item.first.morning && item.first.morning.id ? 'seted' : '' ">早餐</view>
						<view class="time-item" @click="setContent" :data-index="index" :data-item="item" data-type="1" :class="item.first.noon && item.first.noon.id ? 'seted' : '' ">午餐</view>
						<view class="time-item" @click="setContent" :data-index="index" :data-item="item" data-type="2" :class="item.first.evening && item.first.evening.id ? 'seted' : '' ">晚餐</view>
					</block>
					<block v-if="projectIndex == 1">
						<view class="time-item" @click="setContent" :data-index="index" :data-item="item" data-type="0" :class="item.second.morning && item.second.morning.id ? 'seted' : '' ">早餐</view>
						<view class="time-item" @click="setContent" :data-index="index" :data-item="item" data-type="1" :class="item.second.noon && item.second.noon.id ? 'seted' : '' ">午餐</view>
						<view class="time-item" @click="setContent" :data-index="index" :data-item="item" data-type="2" :class="item.second.evening && item.second.evening.id ? 'seted' : '' ">晚餐</view>
					</block>
				</view>
			</block>
		</view>
		<jhxRestaurant ref="food" @success="setSuccess"></jhxRestaurant>
	</view>
</template>

<script>
	import jhxRestaurant from '@/components/jhx-restaurant/setMenu.vue';
	import {
		menuAdd,menuList
	} from '@/api/merchant/menu.js';
	export default {
		components: {jhxRestaurant},
		data() {
			return {
				week:[],
				current:1, //1是本周2是下一周
				project:[
					{name:'方案一',id:0},
					{name:'方案二',id:1}
				],
				projectIndex:0,
			}
		},
		onLoad(options) {
			this.getWeek();
			
		},
		methods: {
			changeProject(index){
				this.projectIndex = index;
			},
			last:function(){
				this.current = 1;
				this.week = [];
				this.getWeek();
			},
			next:function(){
				this.current = 2;
				this.week = [];
				this.getWeek();
			},
			getLog:function(){
				var that = this;
				var param = {
					start_date:this.week[0].date,
					expand:'option'
				};
				uni.showLoading({
					title:'获取记录中'
				})
				this.$http.get(`${menuList}`, param)
					.then(async res => {
					uni.hideLoading();
					that.log = res.data;
					if(that.log){
						that.log.forEach(function(log,index){
							var date = new Date(log.date*1000); //获取一个时间对象
							var month = date.getMonth()+1;  // 获取月份(0-11,0代表1月,用的时候记得加上1)
							if(month<10){
								month = 0+String(month);
							}
							var day = date.getDate();  // 获取日(1-31)
							if(day<10){
								day = 0+String(day);
							}
							var mydate = month+'-'+day;
							that.week.forEach(function(item,index){
								if(item.date == mydate){
									if(log.option.project == 0){
										if(log.type == 0){
											item.first.morning = log;
											that.$set(that.week,index,item);
											
										}
										if(log.type == 1){
											item.first.noon = log;
											that.$set(that.week,index,item);
										}
										if(log.type == 2){
											item.first.evening = log;
											that.$set(that.week,index,item);
										}
									}else if(log.option.project == 1){
										if(log.type == 0){
											item.second.morning = log;
											that.$set(that.week,index,item);
											
										}
										if(log.type == 1){
											item.second.noon = log;
											that.$set(that.week,index,item);
										}
										if(log.type == 2){
											item.second.evening = log;
											that.$set(that.week,index,item);
										}
									}
									
								}
							})
						})
					}
				})
			},
			setContent:function(e){
				var item = this.getDataSet(e,'item');
				var type = this.getDataSet(e,'type');
				var index = this.getDataSet(e,'index');
				var log = '';
				var id = '';
				if(this.projectIndex == 0){
					if(type == 0){
						if(item.first.morning){
							log = item.first.morning;
						}
					}
					if(type == 1){
						if(item.first.noon){
							log = item.first.noon;
						}
					}
					if(type == 2){
						if(item.first.evening){
							log = item.first.evening;
						}
					}
				}else if(this.projectIndex == 1){
					if(type == 0){
						if(item.second.morning){
							log = item.second.morning;
						}
					}
					if(type == 1){
						if(item.second.noon){
							log = item.second.noon;
						}
					}
					if(type == 2){
						if(item.second.evening){
							log = item.second.evening;
						}
					}
				}
				
				this.$refs.food.open(item,type,log,index,this.projectIndex);
			},
			setSuccess:function(e){
				this.$set(this.week,e.index,e.data);
				console.log(this.week);
				//this.week[e.index] = e.data;
			},
			//：
			getWeek() {
				let now = new Date(); // 当前日期
				let nowDayOfWeek = now.getDay(); // 今天本周的第几天
				let day = nowDayOfWeek || 7;
				let nowDay = now.getDate(); // 当前日
				let nowMonth = now.getMonth(); // 当前月
				var start =  Date.parse(new Date(now.getFullYear(), nowMonth, nowDay + 1 - day));//本周起始时间戳
				if(this.current == 2){
					start = start + 7*24*3600*1000;//下周起始时间戳
				}
				console.log(start);
				var oneDay = 24 * 3600 * 1000;
				for(var i = 0; i < 5;i++){
					this.formatDate(new Date(start+i*oneDay));
				}
				this.getLog();
			},
			// 日期格式化
			formatDate(date) {
				let myyear = date.getFullYear();
				
				let mymonth = date.getMonth() + 1;
				let myweekday = date.getDate();
				if (mymonth < 10) {
					mymonth = '0' + String(mymonth);
				}
				if (myweekday < 10) {
					myweekday = '0' + String(myweekday);
				}
				var weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];  
				var myDate = new Date(Date.parse(date));  
				var weekName =  weekDay[myDate.getDay()]
				this.week.push({'date':mymonth + '-' + myweekday,'weekName':weekName,'first':{},'second':{}});
			}
		},
	}
</script>

<style lang="scss">
	.last{display: flex;justify-content: flex-end;margin-bottom: 30rpx;}
	.next{display: flex;justify-content: flex-end;margin-bottom: 30rpx;}
	.container{padding: 40rpx 3%;background-color: #fff;}
	.list-box{
		display: flex;border: 1px solid #ccc;
		.col{width: 20%;border-right: 1px solid #ccc;}
		.head{
			padding: 40rpx 0;text-align: center;
			.date{margin-bottom: 15rpx;}
		}
		.seted{background-color: #dafdf1;}
		.time-item{border-top: 1px solid #ccc;padding: 50rpx 0;text-align: center;}
	}
	.projects{
		display: flex;margin-top: 20rpx;
		.item{width: 200rpx;padding: 25rpx 0;text-align: center;border: 1px solid #ececec; margin-right: 30rpx;border-radius:10rpx;}
		.active{background-color:#ff6600; color: #fff;}
	}
</style>